package com.dao;

import com.beans.Prospects;
import com.beans.Users;
import com.utils.HibernateUtils;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ProspectDao {
	

    public ProspectDao() {
   
        
    }

    public void addPropect(Prospects prospect) {

        Session session = HibernateUtils.currentSession();

        Transaction tx = session.beginTransaction();

        session.save(prospect);

        tx.commit();
        HibernateUtils.closeSession();

    }

    public Prospects findProspect(String numeroprs) {

        Session session = HibernateUtils.currentSession();

        Query query = session.createQuery("from Prospects where prsNumero="
                + numeroprs );
        List<Prospects> listProspect = query.list();

        HibernateUtils.closeSession();

        return listProspect.get(0);

    }
   //méthode qui permet de faire la mise à jour d'un prospect
    
    public boolean updateProspect(Prospects prospect) {
    	
  		boolean updated = false;
  		Transaction tx = null;
  		try {
  			Session session = HibernateUtils.currentSession();// ouverture d'une
  			// session

  			tx = session.beginTransaction();// on ouvre une transaction

  			session.update(prospect);
  			updated = true;

  		} catch (Exception e) {
  			System.out.println("error lors de la mise à jour  du prospect "
  					+ e.getLocalizedMessage());
  		} finally {
  			tx.commit();
  			HibernateUtils.closeSession();
  		}

  		return updated;
      }

    //méthode qui permet la suppresion d'un prospect
    public boolean deleteProspect(Prospects prospect) {
		boolean deleted = false;
		Transaction tx = null;
		try {
			Session session = HibernateUtils.currentSession();// ouverture d'une
			// session

			tx = session.beginTransaction();// on ouvre une transaction

			session.delete(prospect);
			deleted = true;

		} catch (Exception e) {
			System.out.println("error lors de la suppression du prospect "
					+ e.getLocalizedMessage());
		} finally {
			tx.commit();
			HibernateUtils.closeSession();
		}

		return deleted;
    }
    //methode permettant de lister l'ensemble des prospects
    public List<Prospects> selectAllProspects() {
		
		Session session = HibernateUtils.currentSession();
		Query query = session.createQuery("from Prospects");
		List<Prospects> listProspect = query.list();
		for (int i = 0; i < listProspect.size(); i++) {
			Prospects prospect=new Prospects();
			prospect = listProspect.get(i);
			
		}
		return listProspect;
	}
}
